Connecting to Cassandra Database via cqlsh
This article describes how to connect to Cassandra database remotely using cqlsh.
What is cqlsh?
cqlsh is a Python-based command-line client for running CQL commands on a cassandra cluster. Normally it's packaged as part of the full Apache Cassandra™ installation.
Connect to cassandra using cqlsh without SSL
1. Install cqlsh in Linux Server
As cqlsh is packaged as part of the full Apache Cassandra™ installation, so we just need to install Cassandra on server.
Execute command below to perform actions:
Add the Cassandra Repository File
echo "deb http://www.apache.org/dist/cassandra/debian 39x main" | sudo tee -a /etc/apt/sources.list.d/cassandra.sources.list
Add the GPG KEY
sudo apt install curl
curl https://www.apache.org/dist/cassandra/KEYS | sudo apt-key add -
Install Cassandra Server
sudo apt update
sudo apt install cassandra
Enable and Start Cassandra
sudo systemctl enable cassandra
sudo systemctl start cassandra
Verify the Cassandra Installation
sudo systemctl status cassandra
2. Verify cqlsh with the command
cqlsh -h
or
cqlsh -u cassandra -p cassandra
3. Connect to Cassandra via commands
cqlsh <host> <port> -u <user> -p <password>
The <host>
and <port>
information can be get on the Overview page and DB & User page.
Replace the <user>
and <password>
with your own username and password. If you have yet to create your databases(keyspaces) and users, please refer to Creating Keyspaces and users.
For example:
cqlsh cassandra-5049-0.tripanels.com 5050 -u myuser -p Test123go
Connect to cassandra using cqlsh with SSL
1. Install cqlsh in Linux Server
Follow steps 1-2 above in "Connect to Cassandra using cqlsh without SSH".
2. Download certificate file
Download the User Certificate from the connection information section on the Overview page to your PC.
For example, we can put it at /ssl/user.cer.pem.
3. Generate a file named cqlshrc
Create a file named cqlshrc in the /root/.cassandra/
directory. It contains the content below:
[authentication]
username = myuser
password = Test123go
[connection]
hostname = cassandra-5049-0.tripanels.com
port = 5050
factory = cqlshlib.ssl.ssl_transport_factory
[ssl]
certfile = /ssl/user.cer.pem
# Optional, true by default
validate = true
# The next 2 lines must be provided when require_client_auth = true in the cassandra.yaml file
#userkey = /ssl/user.key.pem
#usercert = /ssl/user.cer.pem
[certfiles]
# Optional section, overrides the default certfile in the [ssl] section for 2 way SSL
#172.31.10.22 = ~/ssl/user.cer.pem
#172.31.8.141 = ~/ssl/user.cer.pem
Note: Be sure to replace the username, password, hostname and port with yours. If you have yet to create your databases(keyspaces) and users, please refer to Creating Keyspaces and users.
4. Connect to Cassandra via cqlsh with SSL
Now, we are ready to connect to Cassandra via cqlsh with SSL.
We do not need to enter the username, password, hostname and port in the command. Instead, we will execute the command below.
cqlsh --ssl
It will use the settings defined in the cqlshrc file we created in the last step.
That's it. You've connected to your Cassandra via cqlsh with SSL.
Feel free to contact us if you have any questions.